package controller;

import entity.User;
import entity.Xinwen;
import service.XinwenService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/addx")
public class XinwenaddServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User u= (User) req.getSession().getAttribute("user");
        System.out.println(u.getId());
        if (u.getId().equals("admin")){
        req.getRequestDispatcher("/WEB-INF/jsp/addx.html")
                .forward(req, resp);
        }
        else{
            req.getRequestDispatcher("/WEB-INF/jsp/管理异常.html")
                    .forward(req, resp);
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
//        System.out.println(Integer.parseInt(req.getParameter("id"))+req.getParameter("name")+req.getParameter("link"));
        XinwenService.addXinwen(Integer.parseInt(req.getParameter("id")),req.getParameter("name"),req.getParameter("link"));
        List<Xinwen> xinwens= XinwenService.listUsers();
        req.setAttribute("xinwens", xinwens);
        req.getRequestDispatcher("/WEB-INF/jsp/home.jsp")
                .forward(req, resp);
    }
}